#!/usr/bin/env python3
import sys
import pymysql.cursors

def get_row_count(host, user, password, database, table):
    try:
        # 建立数据库连接
        connection = pymysql.connect(
            host=host,
            user=user,
            password=password,
            database=database,
            charset='utf8mb4',
            cursorclass=pymysql.cursors.DictCursor
        )

        with connection.cursor() as cursor:
            # 执行查询
            sql = f"SELECT COUNT(1) AS count FROM `{table}` WHERE status = 1 AND pre_end_time < TIMESTAMPADD(MINUTE, -5, NOW())"
            cursor.execute(sql)
            result = cursor.fetchone()
            if result:
                return result['count']
            else:
                return 0

    except Exception as e:
        print(f"Error: {e}", file=sys.stderr)
        return -1
    finally:
        # 关闭连接
        if connection:
            connection.close()

if __name__ == "__main__":
    if len(sys.argv) != 6:
        print("Usage: python3 get_count_table.py <host> <user> <password> <database> <table>", file=sys.stderr)
        sys.exit(1)

    host, user, password, database, table = sys.argv[1:6]
    row_count = get_row_count(host, user, password, database, table)
    print(row_count)
